home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / ImageSequence.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  4.5 KB  |  151 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.applet.Applet;
  19.  
  20. /* 
  21.  * This applet displays several images in a row.  It prevents
  22.  * flashing by double buffering.  However, it doesn't wait until
  23.  * the images are fully loaded before drawing them, which causes
  24.  * the weird effect of the animation appearing from the top down.
  25.  */
  26.  
  27. public class ImageSequence extends Applet implements Runnable {
  28.     int frameNumber = -1;
  29.     int delay;
  30.     Thread animatorThread;
  31.     boolean frozen = false;
  32.  
  33.     Dimension offDimension;
  34.     Image offImage;
  35.     Graphics offGraphics;
  36.  
  37.     Image[] images;
  38.  
  39.     public void init() {
  40.         String str;
  41.         int fps = 10;
  42.  
  43.         //How many milliseconds between frames?
  44.         str = getParameter("fps");
  45.         try {
  46.             if (str != null) {
  47.                 fps = Integer.parseInt(str);
  48.             }
  49.         } catch (Exception e) {}
  50.         delay = (fps > 0) ? (1000 / fps) : 100;
  51.  
  52.         //Load all the images.
  53.         images = new Image[10];
  54.         for (int i = 1; i <= 10; i++) {
  55.             images[i-1] = getImage(getCodeBase(),
  56.                           "../../../images/duke/T"+i+".gif");
  57.         }
  58.     }
  59.  
  60.     public void start() {
  61.         if (frozen) {
  62.             //Do nothing.  The user has requested that we
  63.             //stop changing the image.
  64.         } else {
  65.             //Start animating!
  66.             if (animatorThread == null) {
  67.                 animatorThread = new Thread(this);
  68.             }
  69.             animatorThread.start();
  70.         }
  71.     }
  72.  
  73.     public void stop() {
  74.         //Stop the animating thread.
  75.         animatorThread = null;
  76.  
  77.         //Get rid of the objects necessary for double buffering.
  78.         offGraphics = null;
  79.         offImage = null;
  80.     }
  81.  
  82.     public boolean mouseDown(Event e, int x, int y) {
  83.         if (frozen) {
  84.             frozen = false;
  85.             start();
  86.         } else {
  87.             frozen = true;
  88.  
  89.             //Instead of calling stop(), which destroys the 
  90.             //backbuffer, just stop the animating thread.
  91.             animatorThread = null;
  92.         }
  93.         return true;
  94.     }
  95.  
  96.     public void run() {
  97.         //Just to be nice, lower this thread's priority
  98.         //so it can't interfere with other processing going on.
  99.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  100.  
  101.         //Remember the starting time.
  102.         long startTime = System.currentTimeMillis();
  103.  
  104.         //This is the animation loop.
  105.         while (Thread.currentThread() == animatorThread) {
  106.             //Advance the animation frame.
  107.             frameNumber++;
  108.  
  109.             //Display it.
  110.             repaint();
  111.  
  112.             //Delay depending on how far we are behind.
  113.             try {
  114.                 startTime += delay;
  115.                 Thread.sleep(Math.max(0, 
  116.                                       startTime-System.currentTimeMillis()));
  117.             } catch (InterruptedException e) {
  118.                 break;
  119.             }
  120.         }
  121.     }
  122.  
  123.     public void paint(Graphics g) {
  124.         update(g);
  125.     }
  126.  
  127.     public void update(Graphics g) {
  128.         Dimension d = size();
  129.  
  130.         //Create the offscreen graphics context, if no good one exists.
  131.         if ( (offGraphics == null)
  132.           || (d.width != offDimension.width)
  133.           || (d.height != offDimension.height) ) {
  134.             offDimension = d;
  135.             offImage = createImage(d.width, d.height);
  136.             offGraphics = offImage.getGraphics();
  137.         }
  138.  
  139.         //Erase the previous image.
  140.         offGraphics.setColor(getBackground());
  141.         offGraphics.fillRect(0, 0, d.width, d.height);
  142.         offGraphics.setColor(Color.black);
  143.  
  144.         //Paint the frame into the image.
  145.         offGraphics.drawImage(images[frameNumber % 10], 0, 0, this);
  146.  
  147.         //Paint the image onto the screen.
  148.         g.drawImage(offImage, 0, 0, this);
  149.     }
  150. }
  151.